package com.suixinsanren.design.compound.demo02;

import java.util.ArrayList;
import java.util.List;

public class Flock implements QuackAble {

    private List<QuackAble> quackAbles = new ArrayList<>();
    private Observable observable;

    public Flock() {
        quackAbles = new ArrayList<>();
        observable = new Observable(this);
    }

    public void add(QuackAble quackAble) {
        quackAbles.add(quackAble);
    }

    @Override
    public void quack() {
        quackAbles.forEach(QuackAble::quack);
    }

    @Override
    public void addObserver(Observer observer) {
        quackAbles.forEach(item -> item.addObserver(observer));
    }

    @Override
    public void notifyObserver() {
        quackAbles.forEach(QuackObservable::notifyObserver);
    }
}
